﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Bussiness;

public partial class StartExperiment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Timer1.Enabled = false;
            BindReportName();
            this.Session["bl"] = 0;

            Bussiness.Config config = new Config();
            DataTable dt = config.ReadConfig();
            if (dt != null && dt.Rows.Count > 0)
            {
                this.hddMinSplitTime.Value = dt.Rows[0][2].ToString();
                this.txtSplitTime.Value = dt.Rows[0][2].ToString();
                this.hddMaxTime.Value = dt.Rows[0][5].ToString();
                this.txtSYTime.Value = dt.Rows[0][4].ToString();

            }

            Bussiness.ThermalTestReport report = new ThermalTestReport();
            if (report.IsDoingExpriment())
                this.btnStartEnd.Enabled = false;

            this.btnView.Enabled = false;
            this.btnView.Attributes.Add("onclick", "linkExpriment()");
        }
    }
    /// <summary>
    /// 绑定实验名
    /// </summary>
    public void BindReportName()
    {
        ThermalTestTemplate template = new ThermalTestTemplate();
        //template.UnitID = "0";
        template.UnitID = this.ddlComputeID.SelectedValue;
        DataTable dt = template.GetReportName();
        this.ddlSYName.DataSource = dt;
        this.ddlSYName.DataTextField = "TemplateName";
        //this.ddlSYName.DataValueField = "TemplateID";
        this.ddlSYName.DataBind();
    }
    /// <summary>
    /// 开始实验
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnStartEnd_Click(object sender, EventArgs e)
    {
        ThermalTestReport report = new ThermalTestReport();

        if (btnStartEnd.Text == "开始实验")
        {
            if (report.IsDoingExpriment())
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "msg1", "alert('已有实验正在运行，请稍后运行实验...');", true);
                return;
            }
            this.btnView.Enabled = false;

            ThermalTestTemplate template = new ThermalTestTemplate();
            report.TESTTemplateID = template.GetTemplateID(this.ddlSYName.SelectedValue, this.ddlComputeID.SelectedValue);
            report.ReportName = this.txtReportName.Value;
            report.Tester = this.txtSYR.Value;
            report.TestBegin = Convert.ToDateTime(this.calSYStartTime.Text);
            report.TestCalBegin = Convert.ToDateTime(DateTime.Now.ToLongTimeString());
            report.TestDuration = Convert.ToInt32(this.txtSYTime.Value);
            report.SampleInterval = Convert.ToInt32(this.txtSplitTime.Value);
            Bussiness.ThermalTestReport.UserState userState = ThermalTestReport.UserState.New;
            report.InsertTest(userState);

            this.Timer1.Interval = 1000;
            this.Timer1.Enabled = true;
            btnStartEnd.Text = "终止实验";

            this.Label1.Text = this.txtReportName.Value + "实验正在进行已完成0%";

        }
        else
        {
            this.Timer1.Enabled = false;
            int curReportId = report.GetMaxReportId();
            report.EndTest(curReportId);
            btnStartEnd.Text = "开始实验";
            report.EndTest(curReportId);
            this.Timer1.Enabled = true;
            btnStartEnd.Enabled = false;
            this.Label1.Text = "实验已中止";
        }
    }
    /// <summary>
    /// 实时更新实验进度
    /// </summary>
    public void UpdateState()
    {
        ThermalTestReport report = new ThermalTestReport();
        report.TestBegin = Convert.ToDateTime(this.calSYStartTime.Text);
        int curReportId = report.GetMaxReportId();
        string state = report.GetTest(curReportId).Rows[0][10].ToString();
        double roral = report.GetDiffTime(curReportId) / Convert.ToInt32(this.txtSYTime.Value) * 100;
        if (state != "4")
        {
            this.Label1.Text = this.txtReportName.Value + "实验正在进行已完成" + roral.ToString("0") + "%";
        }
        else
        {
            this.Label1.Text = this.txtReportName.Value + "实验已完成100%";
            btnStartEnd.Text = "开始实验";
            this.btnView.Enabled = true;
            this.hddMaxReportID.Value = curReportId.ToString();
            this.Timer1.Enabled = false;

        }

    }
    /// <summary>
    /// 时间触发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        UpdateState();
    }
}
